package bank;

import jaas.MyPrincipal;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import session.AccountManager;
import session.AccountOffice;
import beans.CommonBean;
import beans.ErrorBean;
import beans.InfoBean;
import beans.NavigationBean;
import beans.OverzKlantgegevensBean;
import beans.OverzTransactiesBean;

public class UberClass
{
	public static final String rootDir = "ABCBank";

	public static final int KLANT = 0;
	public static final int MEDEWERKER = 1;
	public static final int PIN = 2;

	private final HttpServletRequest request;
	private final HttpServletResponse response;

	private boolean redirect = false;

	private int type;
	private final HttpSession session;

	public CommonBean vars = new CommonBean();
	private ErrorBean error;
	private InfoBean info;
	private AccountOffice office;
	private AccountManager manager;

	public UberClass(HttpServletRequest r, HttpServletResponse rs) throws ServletException, IOException
	{
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<< NEW REQUEST >>>>>>>>>>>>>>>>>>>>>>>");
		request = r;
		response = rs;
		session = request.getSession();
		if(session.getAttribute("type") != null)
			type = (Integer)session.getAttribute("type");
		else
			type = -1;

		init();
	}

	public UberClass(HttpServletRequest r, HttpServletResponse rs, int t) throws ServletException, IOException
	{
		System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<< NEW REQUEST >>>>>>>>>>>>>>>>>>>>>>>");
		request = r;
		response = rs;
		session = request.getSession();
		type = t;
		setType(type);
		init();
	}

	private void setType(int type)
	{
		session.setAttribute("type", type);
	}

	public int getType()
	{
		return type;
	}

	public void error(String msg)
	{
		if(error == null)
			error = new ErrorBean(msg);
		else
			error.addError(msg);

		vars.remove("showerrors");
		vars.put( "showerrors", "true" );
	}
	public void info(String msg)
	{
		if(info==null)
			info = new InfoBean(msg);
		else
			info.addInfo(msg);
		vars.remove("showinfo");
		vars.put("showinfo", "true");
	}

	public void startOutput()
	{
		System.out.println("<<<<<< START OUTPUT >>>>>>");
		try {
			Thread.sleep(100);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// Openstaande transacties tonen

		if(type == KLANT || type == PIN)
		{
			getOffice().refresh();

			OverzTransactiesBean transacties = new OverzTransactiesBean(getOffice().getPendingTransactions());
			request.setAttribute("transactie", transacties);

			// Klantgegevens tonen

			vars.put("showklantgegevens", "true");
			OverzKlantgegevensBean klantgegevens = new OverzKlantgegevensBean();
			String[] data;
			NumberFormat nf = NumberFormat.getInstance(Locale.US);
			nf.setMaximumFractionDigits(2);
			nf.setMinimumFractionDigits(2);
			data = getOffice().getDetails();
			data[1] = "" + nf.format(Double.parseDouble(data[1]));
			klantgegevens.put("limit",data[2]);
			klantgegevens.put("naam",data[3]);
			klantgegevens.put("saldo", data[1]);
			klantgegevens.put("klantnummer",data[0]);
			request.setAttribute("b_kl", klantgegevens);
		}

		request.setAttribute("vars", vars);
		if(error != null)
			request.setAttribute("errors", error);
		try
		{
			System.out.println(redirect);
			if(!redirect)
			{
				RequestDispatcher r = request.getRequestDispatcher("HoofdScherm.jsp");
				System.out.println(" ");
				System.out.println(" ");
				System.out.println(" ");
				r.forward( request, response );
			}
		}
		catch ( ServletException e )
		{
			e.printStackTrace();
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
	}

	public void init()
	{
		if(type == -1)
		{
			try
			{
				response.sendRedirect("klant");
				redirect = true;
				return;
			}
			catch ( IOException e )
			{
				e.printStackTrace();
			}
		}
		vars.put("showerrors", "false");
		vars.put("showinfo", "false");
		vars.put("showklantgegevens", "false");
		MyPrincipal p = (MyPrincipal)request.getUserPrincipal();
		if(type == MEDEWERKER)
		{
			manager = (AccountManager) p.getUserObject();
			System.out.println("UberClass: SetManager");
		}
		else
		{
			office = (AccountOffice) p.getUserObject();
			office.refresh();

			System.out.println("UberClass: SetOffice");
		}


		/*
		 * Create navigation bar
		 */

		NavigationBean links = new NavigationBean();
		links.addLink("Log uit", "logout");
		if(type == KLANT)
		{
			links.addLink("Openstaande Transacties", "overzicht_transacties");
			links.addLink("Overboeken", "overboeken");
		}
		else if(type == MEDEWERKER)
		{
			links.addLink("Accountbeheer", "accountbeheer");
			links.addLink("Status bank", "status_bank");
		}
		else if(type == PIN)
		{
			//links.addLink("Saldo bekijken", "SaldoBekijken");
			links.addLink("Storten", "storten");
			links.addLink("Opnemen", "opnemen");
		}
		request.setAttribute( "nav", links );
	}

	public AccountManager getManager()
	{
		return manager;
	}

	public AccountOffice getOffice()
	{
		return office;
	}

	public void setVar(String var, String value)
	{
		if(vars.containsKey(var))
			vars.remove( var );
		vars.put( var, value );
	}
}
